
/**
 * Write a description of class Admin here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Admin
{
    public static void main(String[] args){
        Admin maroney = new Admin();
        maroney.getIDs();
        maroney.readPrev();
        maroney.tallyVotes();
    }

    ArrayList<String> fileNames = new ArrayList();
    ArrayList<Category> categories = new ArrayList();
    
    Admin(){
    }
    
    public void getIDs(){
        try{
            Scanner idReader = new Scanner(new File("ID.txt"));
            while(idReader.hasNextLine()){
                fileNames.add(idReader.nextLine());
            }
            idReader.close();
        
            Scanner cats = new Scanner(new File("Categories.txt"));
            while(cats.hasNextLine()){
                categories.add(new Category(cats.nextLine()));
            }
            cats.close();
        }catch(IOException e){
            System.err.println("Unable to locate files.");
        }
    }
    
    public void readPrev(){
        //this method will be designed to work with checkId to get previous votes
        for(String readID: fileNames){
            File toRead = new File(readID + ".txt");
            if(toRead.exists()){
                try{
                    Scanner reader = new Scanner(toRead);
                    String[] mVotes = reader.nextLine().split(";");
                    String[] fVotes = (reader.nextLine()).split(";");
                    reader.close();
                    Vote currVote = new Vote(readID, mVotes, fVotes);
                    currVote.castVote(categories);
                }catch(IOException e){
                    
                }
            }
        }
    }
    
    public void tallyVotes(){
        for(Category c: categories){
            Candidate[] topMen = c.topMen();
            Candidate[] topWomen = c.topWomen();
            String name = c.getName();
            System.out.println("Top 3 males in " + name + ": ");
            for(int i = 0; i < topMen.length; i++){
                if(topMen[i] == null){
                    continue;
                }
                 System.out.println(topMen[i] + " with " + topMen[i].getNumVotes() + " votes");
            }
            System.out.println();
            
            System.out.println("Top 3 females in " + name + ": ");
            for(int i = 0; i < topWomen.length; i++){
                if(topWomen[i] == null){
                    continue;
                }
                 System.out.println(topWomen[i] + " with " + topWomen[i].getNumVotes() + " votes");
            }
            System.out.println();
        }
    }
}
